{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Discovered device  54:6c:0e:b1:5e:c0\n",
      "Discovered device  30:fe:a2:2b:c4:f3\n",
      "Discovered device  54:6c:0e:b1:5d:73\n",
      "Discovered device  54:6c:0e:b1:5e:ee\n",
      "Discovered device  7f:94:53:17:66:1a\n",
      "Discovered device  0c:90:be:58:3b:5b\n",
      "Discovered device  29:df:74:8e:83:37\n",
      "Discovered device  54:6c:0e:b1:5d:57\n",
      "Discovered device  66:32:02:a5:57:5d\n",
      "Received new data from 54:6c:0e:b1:5d:73\n",
      "Discovered device  48:28:fb:10:b4:87\n",
      "Discovered device  58:2d:34:30:36:d1\n",
      "Discovered device  3c:7a:ff:a8:92:aa\n",
      "Discovered device  23:18:22:8e:e3:81\n",
      "Discovered device  6b:dc:ed:9c:a9:16\n",
      "Discovered device  fe:9a:04:e0:34:cb\n",
      "Device 54:6c:0e:b1:5e:ee (public), RSSI=-62 dB\n",
      "  Flags = 06\n",
      "  Incomplete 16b Services = 000018aa-0000-1000-8000-00805f9b34fb\n",
      "  Tx Power = 00\n",
      "  0x12 = 50002003\n",
      "  Complete Local Name = Ours_EEG_Band\n",
      "Device 23:18:22:8e:e3:81 (random), RSSI=-85 dB\n",
      "  Manufacturer = 0600010920029af11fea881b855683456e862f783015f9071bf71e9e93\n",
      "Device 3c:7a:ff:a8:92:aa (random), RSSI=-78 dB\n",
      "  Manufacturer = 0600010920001f95d3e12e963d5bd236077a0098b76f3765a1b595ece8\n",
      "Device 7f:94:53:17:66:1a (random), RSSI=-70 dB\n",
      "  Flags = 1a\n",
      "  Manufacturer = 4c000c0e083f756bc061fc1243b4af746c7410050b1c86f1bc\n",
      "Device 54:6c:0e:b1:5e:c0 (public), RSSI=-60 dB\n",
      "  Flags = 06\n",
      "  Incomplete 16b Services = 000018aa-0000-1000-8000-00805f9b34fb\n",
      "  Tx Power = 00\n",
      "  0x12 = 50002003\n",
      "  Complete Local Name = Ours_EEG_Band\n",
      "Device 0c:90:be:58:3b:5b (random), RSSI=-71 dB\n",
      "  Manufacturer = 0600010920024a932d8c2ef1c17581e1190c2f882be78d3cca2e3e4df4\n",
      "Device 54:6c:0e:b1:5d:73 (public), RSSI=-70 dB\n",
      "  Flags = 06\n",
      "  Incomplete 16b Services = 000018aa-0000-1000-8000-00805f9b34fb\n",
      "  Tx Power = 00\n",
      "  0x12 = 50002003\n",
      "  Complete Local Name = Ours_EEG_Band\n",
      "Device 48:28:fb:10:b4:87 (random), RSSI=-86 dB\n",
      "  Flags = 1a\n",
      "  Manufacturer = 4c001005071c29db9e\n",
      "Device 66:32:02:a5:57:5d (random), RSSI=-62 dB\n",
      "  Flags = 1a\n",
      "  Manufacturer = 4c00100503187b41ef\n",
      "Device 54:6c:0e:b1:5d:57 (public), RSSI=-67 dB\n",
      "  Flags = 06\n",
      "  Incomplete 16b Services = 000018aa-0000-1000-8000-00805f9b34fb\n",
      "  Tx Power = 00\n",
      "  0x12 = 50002003\n",
      "  Complete Local Name = Ours_EEG_Band\n",
      "Device 58:2d:34:30:36:d1 (public), RSSI=-54 dB\n",
      "  Flags = 06\n",
      "  Complete 16b Services = 0000180f-0000-1000-8000-00805f9b34fb,0000180a-0000-1000-8000-00805f9b34fb\n",
      "  16b Service Data = ffffd772eba0ceec\n",
      "  Complete Local Name = MJ_HT_V1\n",
      "Device fe:9a:04:e0:34:cb (random), RSSI=-75 dB\n",
      "  Flags = 06\n",
      "  Incomplete 16b Services = 0000fee0-0000-1000-8000-00805f9b34fb\n",
      "  Complete Local Name = Mi Band 3\n",
      "  Manufacturer = 570100e3e4aad343c94ad6a0183aff3a44e16102fe9a04e034cb\n",
      "Device 30:fe:a2:2b:c4:f3 (random), RSSI=-66 dB\n",
      "  Manufacturer = 060001092002fab040661aac4f2420707d92c8d29290e0645f13e73c17\n",
      "Device 29:df:74:8e:83:37 (random), RSSI=-63 dB\n",
      "  Manufacturer = 0600010920022f491b6c4a0f1f14f367a25ccc0992b9be6d2dd588b323\n",
      "Device 6b:dc:ed:9c:a9:16 (random), RSSI=-90 dB\n",
      "  Flags = 1a\n",
      "  Manufacturer = 4c0010050b1cad9571\n"
     ]
    }
   ],
   "source": [
    "from bluepy.btle import Scanner, DefaultDelegate\n",
    "\n",
    "class ScanDelegate(DefaultDelegate):\n",
    "    def __init__(self):\n",
    "        DefaultDelegate.__init__(self)\n",
    "\n",
    "    def handleDiscovery(self, dev, isNewDev, isNewData):\n",
    "        if isNewDev:\n",
    "            print(\"Discovered device \",  dev.addr)\n",
    "        elif isNewData:\n",
    "            print(\"Received new data from\", dev.addr)\n",
    "\n",
    "scanner = Scanner().withDelegate(ScanDelegate())\n",
    "devices = scanner.scan(10.0)\n",
    "\n",
    "for dev in devices:\n",
    "    print(\"Device %s (%s), RSSI=%d dB\" % (dev.addr, dev.addrType, dev.rssi) )\n",
    "    for (adtype, desc, value) in dev.getScanData():\n",
    "        print(\"  %s = %s\" % (desc, value) )"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "b'T=21.4 H=35.7\\x00'\n",
      "b'T=21.2 H=36.0\\x00'\n",
      "b'T=21.4 H=35.8\\x00'\n",
      "b'T=21.3 H=36.0\\x00'\n",
      "b'T=21.3 H=35.7\\x00'\n",
      "b'T=21.2 H=35.9\\x00'\n",
      "b'T=21.4 H=35.7\\x00'\n",
      "b'T=21.3 H=35.8\\x00'\n",
      "b'T=21.4 H=35.7\\x00'\n",
      "b'T=21.3 H=35.8\\x00'\n",
      "b'T=21.4 H=35.7\\x00'\n",
      "b'T=21.3 H=35.9\\x00'\n"
     ]
    },
    {
     "ename": "BTLEDisconnectError",
     "evalue": "Device disconnected",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mBTLEDisconnectError\u001b[0m                       Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-3-ff413b3057b0>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     28\u001b[0m     \u001b[0mhumidity\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdelegate\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mhumidity\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     29\u001b[0m     \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"温度：\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mtemperature\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"  湿度：\"\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0mhumidity\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m\"%\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 30\u001b[0;31m \u001b[0mget_data\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-3-ff413b3057b0>\u001b[0m in \u001b[0;36mget_data\u001b[0;34m()\u001b[0m\n\u001b[1;32m     23\u001b[0m     \u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwriteCharacteristic\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m0x10\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mbytearray\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m1\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     24\u001b[0m     \u001b[0;32mwhile\u001b[0m \u001b[0;32mnot\u001b[0m \u001b[0mdelegate\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mreceived\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 25\u001b[0;31m         \u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwaitForNotifications\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m30.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     26\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     27\u001b[0m     \u001b[0mtemperature\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mdelegate\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mtemperature\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.5/dist-packages/bluepy/btle.py\u001b[0m in \u001b[0;36mwaitForNotifications\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    558\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    559\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0mwaitForNotifications\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 560\u001b[0;31m          \u001b[0mresp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_getResp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'ntfy'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;34m'ind'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    561\u001b[0m          \u001b[0;32mreturn\u001b[0m \u001b[0;34m(\u001b[0m\u001b[0mresp\u001b[0m \u001b[0;34m!=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    562\u001b[0m     \u001b[0;32mdef\u001b[0m \u001b[0m_setRemoteOOB\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mself\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maddress\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0maddress_type\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moob_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0miface\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mNone\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.5/dist-packages/bluepy/btle.py\u001b[0m in \u001b[0;36m_getResp\u001b[0;34m(self, wantType, timeout)\u001b[0m\n\u001b[1;32m    405\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    406\u001b[0m         \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 407\u001b[0;31m             \u001b[0mresp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_waitResp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwantType\u001b[0m \u001b[0;34m+\u001b[0m \u001b[0;34m[\u001b[0m\u001b[0;34m'ntfy'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'ind'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    408\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mresp\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    409\u001b[0m                 \u001b[0;32mreturn\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.5/dist-packages/bluepy/btle.py\u001b[0m in \u001b[0;36m_waitResp\u001b[0;34m(self, wantType, timeout)\u001b[0m\n\u001b[1;32m    360\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0;34m'state'\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mresp\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mresp\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'state'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m>\u001b[0m \u001b[0;36m0\u001b[0m \u001b[0;32mand\u001b[0m \u001b[0mresp\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'state'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'disc'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    361\u001b[0m                     \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_stopHelper\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 362\u001b[0;31m                     \u001b[0;32mraise\u001b[0m \u001b[0mBTLEDisconnectError\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Device disconnected\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mresp\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    363\u001b[0m             \u001b[0;32melif\u001b[0m \u001b[0mrespType\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'err'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    364\u001b[0m                 \u001b[0merrcode\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mresp\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'code'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mBTLEDisconnectError\u001b[0m: Device disconnected"
     ]
    }
   ],
   "source": [
    "from bluepy import btle\n",
    "import re\n",
    "\n",
    "class XiomiHygroThermoDelegate(object):\n",
    "    def __init__(self):\n",
    "        self.temperature = None\n",
    "        self.humidity = None\n",
    "        self.received = False\n",
    "\n",
    "    def handleNotification(self, cHandle, data):\n",
    "        print(data)\n",
    "        if cHandle == 14:\n",
    "            m = re.search('T=([\\d\\.]*)\\s+?H=([\\d\\.]*)', ''.join(map(chr, data)))\n",
    "            self.temperature = m.group(1)\n",
    "            self.humidity = m.group(2)\n",
    "            self.received = True\n",
    "\n",
    "def get_data():\n",
    "    address = \"58:2d:34:30:36:d1\"\n",
    "    p = btle.Peripheral(address)\n",
    "    delegate = XiomiHygroThermoDelegate()\n",
    "    p.withDelegate( delegate )\n",
    "    p.writeCharacteristic(0x10, bytearray([1, 0]), True)\n",
    "    while not delegate.received:\n",
    "        p.waitForNotifications(30.0)\n",
    "\n",
    "    temperature = delegate.temperature\n",
    "    humidity = delegate.humidity\n",
    "    print(\"温度：\",temperature,\"  湿度：\",humidity,\"%\")\n",
    "get_data()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [],
   "source": [
    "from bluepy import btle\n",
    "import json\n",
    "BIT0 = 1\n",
    "BIT1 = 0x1 << 1\n",
    "BIT2 =  0x1 << 2\n",
    "BIT3 = 0x1 << 3\n",
    "BIT4 = 0x1 << 4\n",
    "\n",
    "import paho.mqtt.client as mqtt\n",
    " \n",
    "HOST = \"localhost\"\n",
    "PORT = 1883\n",
    "\n",
    "client = mqtt.Client()\n",
    "client.connect(HOST, PORT, 60)\n",
    "\n",
    "class EEGBandDelegate(object):\n",
    "    def __init__(self,address):\n",
    "        self.address = address\n",
    "        self.data = {\"id\":address}\n",
    "\n",
    "    def handleNotification(self, cHandle, data):\n",
    "        print(cHandle,data)\n",
    "        flags = data[0]\n",
    "        i = 1\n",
    "        #fmt.Printf(\"HANDLE_ID_SERVICE1: % x\\n\",data)\n",
    "        if flags & BIT0 == BIT0 :\n",
    "            #Quality\n",
    "            i = i + 1\n",
    "        if flags & BIT1 == BIT1 :\n",
    "            #Attention\n",
    "            self.data['attention'] = data[i]\n",
    "            i = i + 1\n",
    "        if flags & BIT2 == BIT2 :\n",
    "            #Meditation\n",
    "            self.data['meditation'] = data[i]\n",
    "            i = i + 1\n",
    "        if flags & BIT3 == BIT3 :\n",
    "            #BlinkStrength\n",
    "            self.data['blinkStrength'] = data[i]\n",
    "            i = i + 1\n",
    "        print(json.dumps(self.data))\n",
    "        client.publish(\"ble2zigbee/eegband\",json.dumps(self.data),0)\n",
    "connections = []\n",
    "def get_data(address):\n",
    "    p = btle.Peripheral(address)\n",
    "    connections.append(p)\n",
    "    delegate = EEGBandDelegate(address)\n",
    "    p.withDelegate( delegate )\n",
    "    p.writeCharacteristic(0x21, bytearray([1, 0]), True)\n",
    "    try:\n",
    "        while True:\n",
    "            p.waitForNotifications(10.0)\n",
    "    except Exception as e:\n",
    "        print('Exception,',e)\n",
    "    finally:\n",
    "        p.disconnect()  \n",
    "# get_data(address= \"54:6c:0e:b1:5d:73\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Discovered device  15:7e:1b:34:c6:2e\n",
      "Discovered device  77:3b:29:c8:0b:0c\n",
      "Discovered device  10:6a:44:5b:8c:8b\n",
      "Discovered device  54:6c:0e:b1:5d:06\n",
      "Discovered device  54:6c:0e:b1:5e:c0\n",
      "Discovered device  54:6c:0e:b1:5d:5e\n",
      "Discovered device  54:6c:0e:b1:5e:86\n",
      "Discovered device  54:6c:0e:b1:5e:a9\n",
      "Discovered device  54:6c:0e:b1:58:19\n",
      "Discovered device  fe:9a:04:e0:34:cb\n",
      "Received new data from 54:6c:0e:b1:58:19\n",
      "Discovered device  21:e5:52:1f:7b:d4\n",
      "Discovered device  54:6c:0e:b1:5d:73\n"
     ]
    },
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-33-fd6a386d9ffc>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     14\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     15\u001b[0m \u001b[0mscanner\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mScanner\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mwithDelegate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mScanDelegate\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 16\u001b[0;31m \u001b[0mdevices\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mscanner\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mscan\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m10.0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     17\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     18\u001b[0m \u001b[0;32mfor\u001b[0m \u001b[0mdev\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mdevices\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.5/dist-packages/bluepy/btle.py\u001b[0m in \u001b[0;36mscan\u001b[0;34m(self, timeout, passive)\u001b[0m\n\u001b[1;32m    851\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mclear\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    852\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstart\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mpassive\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0mpassive\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 853\u001b[0;31m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mprocess\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    854\u001b[0m         \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    855\u001b[0m         \u001b[0;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mgetDevices\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.5/dist-packages/bluepy/btle.py\u001b[0m in \u001b[0;36mprocess\u001b[0;34m(self, timeout)\u001b[0m\n\u001b[1;32m    819\u001b[0m             \u001b[0;32melse\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    820\u001b[0m                 \u001b[0mremain\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 821\u001b[0;31m             \u001b[0mresp\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_waitResp\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m'scan'\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;34m'stat'\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mremain\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    822\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mresp\u001b[0m \u001b[0;32mis\u001b[0m \u001b[0;32mNone\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    823\u001b[0m                 \u001b[0;32mbreak\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;32m/usr/local/lib/python3.5/dist-packages/bluepy/btle.py\u001b[0m in \u001b[0;36m_waitResp\u001b[0;34m(self, wantType, timeout)\u001b[0m\n\u001b[1;32m    340\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    341\u001b[0m             \u001b[0;32mif\u001b[0m \u001b[0mtimeout\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 342\u001b[0;31m                 \u001b[0mfds\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mself\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0m_poller\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpoll\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mtimeout\u001b[0m\u001b[0;34m*\u001b[0m\u001b[0;36m1000\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m    343\u001b[0m                 \u001b[0;32mif\u001b[0m \u001b[0mlen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mfds\u001b[0m\u001b[0;34m)\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;36m0\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m    344\u001b[0m                     \u001b[0mDBG\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m\"Select timeout\"\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "from bluepy.btle import Scanner, DefaultDelegate\n",
    "\n",
    "dev_list = []\n",
    "\n",
    "class ScanDelegate(DefaultDelegate):\n",
    "    def __init__(self):\n",
    "        DefaultDelegate.__init__(self)\n",
    "\n",
    "    def handleDiscovery(self, dev, isNewDev, isNewData):\n",
    "        if isNewDev:\n",
    "            print(\"Discovered device \",  dev.addr)\n",
    "        elif isNewData:\n",
    "            print(\"Received new data from\", dev.addr)\n",
    "\n",
    "scanner = Scanner().withDelegate(ScanDelegate())\n",
    "devices = scanner.scan(10.0)\n",
    "\n",
    "for dev in devices:\n",
    "    print(\"Device %s (%s), RSSI=%d dB\" % (dev.addr, dev.addrType, dev.rssi) )\n",
    "    for (adtype, desc, value) in dev.getScanData():\n",
    "        if desc == \"Complete Local Name\" and value==\"Ours_EEG_Band\":\n",
    "            dev_list.append(dev.addr)\n",
    "            print(\"find Ours_EEG_Band , num \",len(dev_list))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "ename": "KeyboardInterrupt",
     "evalue": "",
     "output_type": "error",
     "traceback": [
      "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m                         Traceback (most recent call last)",
      "\u001b[0;32m<ipython-input-32-910701079468>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m     22\u001b[0m         \u001b[0;32mfor\u001b[0m \u001b[0mp\u001b[0m \u001b[0;32min\u001b[0m \u001b[0mconnections\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     23\u001b[0m             \u001b[0mp\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mdisconnect\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 24\u001b[0;31m \u001b[0mget_data_muilti\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mdev_list\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[0;32m<ipython-input-32-910701079468>\u001b[0m in \u001b[0;36mget_data_muilti\u001b[0;34m(dev_list)\u001b[0m\n\u001b[1;32m     15\u001b[0m     \u001b[0;32mtry\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     16\u001b[0m         \u001b[0;32mwhile\u001b[0m \u001b[0;32mTrue\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 17\u001b[0;31m             \u001b[0mtime\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msleep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m     18\u001b[0m \u001b[0;31m#             break\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m     19\u001b[0m     \u001b[0;32mexcept\u001b[0m \u001b[0mException\u001b[0m \u001b[0;32mas\u001b[0m \u001b[0me\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
      "\u001b[0;31mKeyboardInterrupt\u001b[0m: "
     ]
    }
   ],
   "source": [
    "import time\n",
    "import threading\n",
    "def get_data_muilti(dev_list):\n",
    "    thread_list = []\n",
    "    time.clock()\n",
    "    # 逐一添加线程\n",
    "    for addr in dev_list:\n",
    "        print(addr)\n",
    "        t = threading.Thread(target=get_data, args=(addr,))\n",
    "        thread_list.append(t)\n",
    "\n",
    "    # 逐一启动线程\n",
    "    for t in thread_list:\n",
    "        t.start()\n",
    "    try:\n",
    "        while True:\n",
    "            time.sleep(20)\n",
    "#             break\n",
    "    except Exception as e:\n",
    "        print('Exception,',e)\n",
    "    finally:\n",
    "        for p in connections:\n",
    "            p.disconnect()\n",
    "get_data_muilti(dev_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
